﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eneta.Portal.Common;
using Eneta.Portal.Common.Dto;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;

namespace Eneta.Portal.Controls
{
    public class UserHtmlDisplayControl : UserControl
    {
        public string Identification { get; set; }

        public bool UserLogedIn { get; set; }
        
        //protected override void CreateChildControls()
        protected override void  OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            var user = SPContext.Current.Web.CurrentUser;
            if (user == null)
            {
                return;
            }

            try
            {
                var dto = GetHtml();
                if (dto == null)
                    return;

                var html = dto.Html;
                if (string.IsNullOrEmpty(html))
                    return;

                var insideCache = IsInsidePartialCachingControl();
                
                var parts = html.Split(new[] {"{User}"}, 1000, StringSplitOptions.None);
                for (var i = 0; i < parts.Length; i++)
                {
                    Controls.Add(new LiteralControl(parts[i]));
                    if (i < parts.Length - 1)
                    {
                        if (insideCache)
                        {
                            Logger.LogWarning("UserHtmlDisplayControl is under cached control. Adding text as literal control.", GetType().Name);
                            Controls.Add(new LiteralControl(user.Name));
                        }
                        else
                        {
                            Logger.LogInfo("Substituted new placeholder with method GetUserName", GetType().Name);
                            Controls.Add(new Substitution {MethodName = "GetUserName"});
                        }                        
                    }
                }
            }
            catch(Exception ex)
            {
                Logger.LogWarning("OnLoad(): Error occured", GetType().ToString());
                Logger.LogException(ex);
            }
        }

        private bool IsInsidePartialCachingControl()
        {
            for (Control control = Parent; control != null; control = control.Parent)
                if (control is BasePartialCachingControl)
                    return true;

            return false;
        }

        public static string GetUserName(HttpContext context)
        {
            var user = SPContext.Current.Web.CurrentUser;
            if (user == null)
                return string.Empty;

            return user.Name;
        }

        protected HtmlDto GetHtml()
        {
            try
            {
                if (string.IsNullOrEmpty(Identification))
                    return null;
                var repository = new HtmlRepository(SPContext.Current.Site.RootWeb);
                return repository.GetHtmlBy(Identification);
            }
            catch (Exception ex)
            {
                Logger.LogWarning("GetHtml(): Error occured", GetType().ToString());
                Logger.LogException(ex);
                return null;
            }
        }
    }
}
